<?php
include_once DIR_DEFS.'database.php';



/**
 * Description of DbMeasure
 *
 * @author MWillems
 * @version 0.1
 * 
 * Version history
 * 0.1  MWillems    Initial version
 */
class DbPrestatie extends Database {
    
    private $id;
    private $name;
    private $desc;
    private $niveau;
    
    public function __construct() {
        parent::__construct();
    }

    public function DbPrestatie($id='') {
        parent::Database();
        
        if ( !$this->dbTableExists(TBL_PRESTATIE)){
            if (!$this->createTable(TBL_PRESTATIE)){
                trigger_error(TXT_ERROR_CANT_CREATE_TABLE. TBL_PRESTATIE, E_USER_ERROR);
            } 
            
        }else if ( (strlen($id) > 0 ) && $this->dbCheckId ($id, FIELD_PRESTATIE_ID) ){
            $this->selectById($id);
        }
        
        
    }
    
    public function checkPrestatieNameLen($value){
        if (trim($value) > LEN_PRESTATIE_NAME){
            return "Too long.";
        } else {
            return TRUE;
        }
    }
    
    /**
     * save the provided vars into the database
     * 
     * @param String $name
     * @param String $crebo
     * @param String $cohort
     * @param String $desc
     * 
     * @return bool if FALSE check the errors
     */
    public function save(   $name, 
                            $desc,
							$niveau){


/**
 * @todo
 * Create database trigger for Id existence
 */
        try{

            $this->checkName($name);
            $this->checkDesc($desc);
            $this->checkNiveau($niveau);

        }

        //catch exception
        catch(Exception $e)
        {
            $this->setError( $e->getMessage() );
            return FALSE;
        }
        
        $query = "INSERT INTO  `".DB_NAME."`.`".TBL_PRESTATIE."` (".
                    "`".FIELD_PRESTATIE_NAME."` ,".
                    "`".FIELD_PRESTATIE_DESC."` ,".
					"`".FIELD_PRESTATIE_NIVEAU."`".
                    ")".
                    "VALUES ('".
                    $this->dbInString($name). "', '".
					$this->dbInString($desc). "', '".
                    $this->dbInString($niveau). " '".
                    ")";
                
        /** Query **/
        $this->dbquery($query);

        if($this->checkDbErrors($query)){
            return FALSE;
        }
        
        /**  Update class vars **/
        $this->id = $this->dbGetInsertedId();
        
        /** Load class vars **/
        return ($this->selectById($this->id));
        
    }
    
    private function checkName($name){
        
        $this->checkText($name, LEN_PRESTATIE_NAME);
    }
    
    private function checkDesc($desc){
        
        $this->checkText($desc, LEN_PRESTATIE_DESC, TRUE);
    }
	
	private function checkNiveau($niveau){
        
        $this->checkText($niveau, LEN_PRESTATIE_NIVEAU, TRUE);
    }

    private function selectById( $id ){
        
        if( $this->CheckId($id, FIELD_PRESTATIE_ID)){
            $query = "SELECT ".
                        "`".FIELD_PRESTATIE_NAME."` ,".
                        "`".FIELD_PRESTATIE_DESC."`".
                     " FROM  `".TBL_PRESTATIE."`". 
                     " WHERE (`".FIELD_PRESTATIE_ID."` = ". $id .")";
            
            $this->dbQuery($query);
        
            if ( $this->checkDbErrors($query) ){
                return FALSE;
            }
                
            $row = $this->dbFetchArray();
            if ( $this->checkDbErrors($query) ){  
                return FALSE;
            }
            
            $this->name = $row[FIELD_PRESTATIE_NAME];
            $this->desc = $row[FIELD_PRESTATIE_DESC];
            $this->niveau = $row[FIELD_PRESTATIE_NIVEAU];
            
            if ( strlen($this->id) < 1 ){
                $this->id = $id;
            }

            return TRUE;
        }
            
    }
    
    private function createTable(){
        
        $query = "CREATE TABLE IF NOT EXISTS `".TBL_PRESTATIE."` (".
                  "`".FIELD_PRESTATIE_ID ."` ".FIELD_PRESTATIE_ID."(".LEN_PRESTATIE_ID.") NOT NULL AUTO_INCREMENT,".
                  "`".FIELD_PRESTATIE_NAME ."` varchar(".LEN_PRESTATIE_NAME.") NOT NULL,".
                  "`".FIELD_PRESTATIE_DESC."` longtext NOT NULL,".
                  "PRIMARY KEY (`".FIELD_PRESTATIE_ID."`),".
                  "UNIQUE KEY `".FIELD_PRESTATIE_NAME."` (`".FIELD_PRESTATIE_NAME."`)".
                ") DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";
        
        $this->dbQuery($query);
        
        return ( !$this->checkDbErrors($query) );
    }

}

class TestDbPrestatie {

    public function TestDbPrestatie() {
        $test_prestatie = new DbPrestatie();
        
        
       echo "<pre>";
       var_dump($test_prestatie);
       echo "</pre>";
    }

}


?>
